$(document).ready
(
	function()
	{
		// 获取body可用宽高
		var body_width = $(window).width();
		var body_height = $(window).height();

		// 获取图片尺寸
		var img_width = $('.image img').width();
		var img_height = $('.image img').height();
		// 判断图片版式（1：横向 2:纵向）
		var img_format;
		if(img_width > img_height)
		{
			img_format = 1;
		}
		else
		{
			img_format = 2;
		}
		// 修正图片尺寸
		var fixed_img_width;
		var fixed_img_height;
		function fix_img_size()
		{
			if( img_width > body_width || img_height > body_height )
			{
				switch(img_format)
				{
					case 1:
						fixed_img_width = body_width;
						fixed_img_height = parseInt((body_width / img_width) * img_height);
						if(fixed_img_height > body_height)
						{
							img_format = 2;
							fix_img_size();
						}
						break;
					case 2:
						fixed_img_height = body_height;
						fixed_img_width = parseInt((body_height / img_height) * img_width);
						if(fixed_img_width > body_width)
						{
							img_format = 1;
							fix_img_size();
						}
						break;
				}
			}
			$('.image img').css
			(
				{
					'width': fixed_img_width + 'px',
					'height': fixed_img_height + 'px'
				}
			);
		}
		// 修正图片位置
		var img_left_padding = 0;
		function fix_img_position()
		{
			if(fixed_img_width < body_width)
			{
				img_left_padding = parseInt((body_width - fixed_img_width) / 2);
				$('.image').css('margin-left', img_left_padding + 'px');
			}
		}
		fix_img_size();
		fix_img_position();

		// 设置翻页按钮行高
		var page_button_line_height = $('.button_prev').height();
		$('.page_button').css('line-height', page_button_line_height + 'px');

		// 设置翻页按钮位置
		function fix_page_button_position()
		{
			var page_button_top_padding = parseInt((fixed_img_height - $('.button_prev').height()) / 2);
			$('.button_prev').css('top', page_button_top_padding + 'px');
			$('.button_next').css('top', page_button_top_padding + 'px');
		}
		fix_page_button_position();

		// 更新缩略图选中状态
		function init_thumb_activation(target_index)
		{
			$('.photo_thumb_list img').css('opacity', 0.4);
			$('.photo_thumb_list img[data-thumb_index=' + target_index + ']').css('opacity', 1);
		}
		init_thumb_activation(target_index);

		// 监听点击翻页按钮
		$('.page_button').click
		(
			function()
			{
				// 隐藏翻页按钮、缩略图列表按钮
				$('.button_prev').css('display', 'none');
				$('.button_next').css('display', 'none');
				$('.button_thumb').css('display', 'none');
				var target_index = $(this).attr('data-target_index');
				// 动态改变URL
				window.history.pushState({}, 0, target_index);
				// 动态改变翻页按钮
				var next_index = parseInt(target_index) + 1;
				if(next_index > photo_total)
				{
					next_index = 1;
				}
				var prev_index = parseInt(target_index) - 1;
				if(prev_index < 1)
				{
					prev_index = photo_total;
				}
				$('.button_prev span').attr('data-target_index', prev_index);
				$('.button_next span').attr('data-target_index', next_index);
				// 获取图片索引URL
				thumb_url = $('.photo_thumb_list img[data-thumb_index=' + (target_index) + ']').attr('src');
				// 分割URL
				thumb_url_arr = thumb_url.split('/');
				// 获取文件名
				file_name = thumb_url_arr[thumb_url_arr.length-1];
				file_name_arr = file_name.split('_');
				if(file_name_arr.length > 2)
				{
					file_name_arr.splice(0, 1);
					file_name = file_name_arr.join('_');
				}
				else
				{
					file_name = file_name_arr[1];
				}
				// 整理URL数组
				thumb_url_arr[thumb_url_arr.length-2] = file_name;
				thumb_url_arr.splice(thumb_url_arr.length-1, 1);
				photo_url = thumb_url_arr.join('/');
				// 修正图片尺寸
				$.post
				(
					HOST_NAME + "Index/get_photo_info",
					{photo_url: thumb_url},
					function(data)
					{
						// 替换图片URL
						var new_photo = new Image();
						new_photo.onload = function()
						{
							var return_data = eval('(' + data + ')');
							// 获取图片尺寸
							img_width = return_data.width;
							img_height = return_data.height;
							// 修正图片尺寸、翻页按钮位置
							fix_img_size();
							fix_img_position();
							fix_page_button_position()
							// 替换图片URL
							$('.image img').attr('src', photo_url);
							// 显示翻页按钮、缩略图列表按钮
							$('.button_prev').css('display', 'block');
							$('.button_next').css('display', 'block');
							$('.button_thumb').css('display', 'block');
						}
						new_photo.src = photo_url;
					}
				);
				// 切换缩略图状态
				init_thumb_activation(target_index);
			}
		);

		// 设置缩略图DIV的尺寸
		$('.photo_thumb_list').css('width', body_width + 'px');
		// 设置缩略图宽度
		var thumb_img_width = parseInt(body_width / 3);
		$('.photo_thumb_list img').css('width', thumb_img_width + 'px');

		// 重排缩略图顺序，以保证在3列瀑布流下，图片顺序在横向上保持不变
		var thumb_amount = $('.photo_thumb_list img').length;
		new_thumb_list = new Array();
		new_thumb_list[0] = new Array();
		new_thumb_list[1] = new Array();
		new_thumb_list[2] = new Array();
		for(i=0; i<thumb_amount; i++)
		{
			var a = (i + 3) % 3;
			new_thumb_list[a].push( $('.photo_thumb_list').children('img')[i].outerHTML );
		}
		new_thumb_list[0] = new_thumb_list[0].join('');
		new_thumb_list[1] = new_thumb_list[1].join('');
		new_thumb_list[2] = new_thumb_list[2].join('');
		new_thumb_list = new_thumb_list.join('');
		$('.photo_thumb_list').html(new_thumb_list);

		// 监听点击“缩略图列表”
		$('.button_thumb').click
		(
			function()
			{
				// 获取按钮状态
				var data_status = $(this).attr('data-status');
				switch(data_status)
				{
					case 'on':
						$(this).attr('data-status', 'off');
						$('.photo_thumb_list').css
						(
							{
								'position': 'unset',
								'display': 'none'
							}
						);
						break;
					case 'off':
						$(this).attr('data-status', 'on');
						$('.photo_thumb_list').css
						(
							{
								'position': 'absolute',
								'display': 'block'
							}
						);
						break;
				}
			}
		);

		// 监听点击缩略图
		$('.photo_thumb_list img').on
		(
			'click',
			function()
			{
				var target_index = $(this).attr('data-thumb_index');
				window.history.pushState({}, 0, target_index);
				window.location.href = window.location.href;
			}
		);

	}
);
